package com.share.device.controller;

import com.share.common.core.web.controller.BaseController;
import com.share.common.core.web.domain.AjaxResult;
import com.share.common.core.web.page.TableDataInfo;
import com.share.device.domain.Cabinet;
import com.share.device.service.ICabinetService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.aspectj.weaver.loadtime.Aj;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 柜机 controller
 */
@Tag(name = "充电宝柜机接口管理")
@RestController
@RequestMapping("/cabinet")
public class CabinetController extends BaseController {

    @Autowired
    private ICabinetService cabinetService;

    //分页查询柜机
    @Operation(summary = "查询充电宝柜机列表")
    @GetMapping("/list")
    public TableDataInfo list(Cabinet cabinet){
        log.info("查询充电宝柜机列表:{}",cabinet);

        startPage();
        List<Cabinet> cabinetList = cabinetService.selectCabinetList(cabinet);
        return getDataTable(cabinetList);
    }

    //获取充电宝柜机详细信息
    @Operation(summary = "获取充电宝柜机详细信息")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfoById(@PathVariable Long id){
        log.info("获取充电宝柜机详细信息:{}",id);

        //mp 会根据del_flag 判断是否被删除
        Cabinet cabinet = cabinetService.getById(id);

        return AjaxResult.success(cabinet);
    }

    //新增充电宝柜机
    @Operation(summary = "新增充电宝柜机")
    @PostMapping
    public AjaxResult save(@RequestBody Cabinet cabinet){
        log.info("新增充电宝柜机:{}",cabinet);

        Boolean isSuccess = cabinetService.saveCabinet(cabinet);

        return toAjax(isSuccess);
    }

    //修改充电宝柜机
    @Operation(summary = "修改充电宝柜机")
    @PutMapping
    public AjaxResult updateById(@RequestBody Cabinet cabinet){
        log.info("修改充电宝柜机:{}",cabinet);

        boolean isSuccess = cabinetService.updateById(cabinet);

        return toAjax(isSuccess);
    }

    //删除充电宝柜机 可批量删除
    @Operation(summary = "删除充电宝柜机")
    @DeleteMapping("/{ids}")
    public AjaxResult delete(@PathVariable List<Long> ids){
        log.info("删除充电宝柜机 可批量删除:{}",ids);

        boolean isSuccess = cabinetService.removeBatchByIds(ids);

        return toAjax(isSuccess);
    }

    //搜索未使用柜机 并且状态为 0
    @Operation(summary = "搜索未使用柜机")
    @GetMapping("/searchNoUseList/{keyword}")
    public AjaxResult selectNoUse(@PathVariable String keyword){
        log.info("搜索未使用柜机");

        List<Cabinet> cabinetList = cabinetService.selectNoUse(keyword);

        return AjaxResult.success(cabinetList);
    }

    @Operation(summary = "获取充电宝柜机全部详细信息 包括柜机中槽位以及对应充电宝信息")
    @GetMapping(value = "/getAllInfo/{id}")
    public AjaxResult getAllInfo(@PathVariable("id") Long id)
    {
        return success(cabinetService.getAllInfo(id));
    }
}
